<template>
  <el-dialog
    v-dialogDrag
    :title="title"
    :visible.sync="visible"
    width="800px"
    append-to-body
  >
    <el-form ref="form" :model="form" label-width="auto" :rules="rules">      

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="标题" prop="title" class="person">
            <el-input
              v-model="form.title"
              placeholder="标题"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="类型" prop="type" class="person">
            <el-radio-group v-model="form.type">
              <el-radio :label="'0'">问答</el-radio>
              <el-radio :label="'1'">视频解读</el-radio>
              <el-radio :label="'2'">案例</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20" v-if="form.type === '0'">
        <el-col :span="24">
          <el-form-item label="回答" prop="details.answer" class="person">
            <el-input
              v-model="form.details.answer"
              placeholder="回答"
              :rows="6"
              type="textarea"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20" v-if="form.type === '1'">
        <el-col :span="24">
          <el-form-item label="解读" prop="details.explain" class="person">
            <el-input
              v-model="form.details.explain"
              placeholder="解读"
              :rows="6"
              type="textarea"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20" v-if="form.type === '1'">
        <el-col :span="24">
          <el-form-item label="视频地址" prop="videoUrl" class="person">
            <el-input
              v-model="form.videoUrl"
              placeholder="视频地址"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20" v-if="form.type === '2'">
        <el-col :span="24">
          <el-form-item label="文件路径" prop="fileList" class="person">
            <FileUpload
              v-model="form.fileList"
              :limit="1"
              :isMultiple="false"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20" v-if="form.type === '2'">
        <el-col :span="24">
          <el-form-item label="标签列表" prop="details.labelList">
            <div class="label-list">
              <el-tag
                v-for="item in form.details.labelList"
                :key="item"
                closable
                @close="handleClose(tag)"
              >
                {{ item }}
              </el-tag>
              <el-input
                v-if="inputVisible"
                v-model="inputValue"
                ref="saveTagInput"
                size="small"
                class="input-new-tag"
                @keyup.enter.native="handleInputConfirm"
                @blur="handleInputConfirm"
              ></el-input>
              <el-button v-else class="button-new-tag" size="small" @click="showInput">+</el-button>
            </div>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20" v-if="form.type === '2'">
        <el-col :span="24">
          <el-form-item label="患者基本信息" prop="details.disease">
            <el-input
              v-model="form.details.disease"
              placeholder="患者基本信息"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20" v-if="form.type === '2'">
        <el-col :span="24">
          <el-form-item label="病史描述" prop="details.change">
            <el-input
              v-model="form.details.change"
              placeholder="病史描述"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row  :gutter="20" v-if="form.type === '2'">
        <el-col :span="12">
          <el-form-item label="案例处理体会" prop="details.diagnosisTime">
            <el-input
              v-model="form.details.diagnosisTime"
              placeholder="案例处理体会"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="影像检查" prop="details.hospital">
            <el-input
              v-model="form.details.hospital"
              placeholder="影像检查"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row  :gutter="20" v-if="form.type === '2'">
        <el-col :span="24">
          <el-form-item label="疗效评估" prop="details.treatment">
            <el-input
              v-model="form.details.treatment"
              placeholder="疗效评估"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row  :gutter="20" v-if="form.type === '2'">
        <el-col :span="24">
          <el-form-item label="治疗方案" prop="details.programme">
            <el-input
              v-model="form.details.programme"
              placeholder="治疗方案"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>

  </el-dialog>
</template>

<script>
import BindDialog from '@/components/bindDialog/index.vue'
import { dialogFrame } from './frameData'
import { modifyQuestion, addQuestion } from '@/api/question'

const markForm = () => {
  return {
    title: "",
    type: "0",
    fileUrl: "",
    videoUrl: "",
    fileList: [],
    details: {
      disease: "",
      change: "",
      diagnosisTime: "",
      hospital: "",
      treatment: "",
      programme: "",
      labelList: [],
      answer: "",
      explain: "",
    }
  }
}

export default {
  name: "",
  components: {
    BindDialog
  },
  data() {
    return {
      rules: {
        title: [{ required: true, message: '标题是必填项', trigger: 'blur' }],
        type: [{ required: true, message: '类型是必填项', trigger: 'change' }],
        'details.answer': [{ required: true, message: '回答是必填项', trigger: 'blur' }]
      },
      title: "",
      type: "",
      config: {
        edit: {
          func: modifyQuestion,
          title: "编辑"
        },
        new: {
          func: addQuestion,
          title: "新增"
        }
      },
      form: markForm(),
      visible: false,
      inputValue: "",
      inputVisible: false,
      dialogFrame,
      query: '',
      loading: false,
      options: []
    };
  },
  watch: {
    'form.type': function(newType) {
      if (newType === '0') {
        this.rules = {
          title: [{ required: true, message: '标题是必填项', trigger: 'blur' }],
          type: [{ required: true, message: '类型是必填项', trigger: 'change' }],
          'details.answer': [{ required: true, message: '回答是必填项', trigger: 'blur' }]
        };
      } else if (newType === '1') {
        this.rules = {
          title: [{ required: true, message: '标题是必填项', trigger: 'blur' }],
          type: [{ required: true, message: '类型是必填项', trigger: 'change' }],
          'details.explain': [{ required: true, message: '解读是必填项', trigger: 'blur' }],
          videoUrl: [{ required: true, message: '视频地址是必填项', trigger: 'blur' }]
        };
      } else if (newType === '2') {
        this.rules = {
          title: [{ required: true, message: '标题是必填项', trigger: 'blur' }],
          type: [{ required: true, message: '类型是必填项', trigger: 'change' }],
          fileList: [{ required: true, message: '文件路径是必填项', trigger: 'change' }],
          'details.labelList': [{ required: true, message: '必填项', trigger: 'blur' }],
          'details.disease': [{ required: true, message: '必填项', trigger: 'blur' }],
          'details.change': [{ required: true, message: '必填项', trigger: 'blur' }],
          'details.diagnosisTime': [{ required: true, message: '是必填项', trigger: 'blur' }],
          'details.hospital': [{ required: true, message: '必填项', trigger: 'blur' }],
          'details.treatment': [{ required: true, message: '必填项', trigger: 'blur' }],
          'details.programme': [{ required: true, message: '必填项', trigger: 'blur' }]
        };
      }
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    }
  },
  methods: {
    handleClose(tag) {
      this.form.details.labelList.splice(this.form.details.labelList.indexOf(tag), 1);
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput?.$refs?.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.form.details.labelList.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = '';
    },

    showDialog(row) {
      this.reset();
      const isEdit = !!row;
      this.visible = true;
      this.type = isEdit ? "edit" : "new";
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
      if (isEdit) {
        this.form = Object.assign(this.form, row);
      }
      this.title = this.config[this.type].title;
    },

    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.visible = false;
          const data = { ...this.form }

          if(this.type === "new"){
            delete data.id;
          }

          if(data.type === '0'){
            data.content = JSON.stringify({
              answer: data.details.answer
            })

            delete data.details
            delete data.videoUrl
            delete data.fileUrl
            delete data.fileList
          }else if(data.type === '1'){
            data.content = JSON.stringify({
              explain: data.details.explain
            })

            delete data.details
            delete data.fileUrl
            delete data.fileList
          }else if(data.type === '2'){
            data.content = JSON.stringify({
              disease: data.details.disease,
              change: data.details.change,
              diagnosisTime: data.details.diagnosisTime,
              hospital: data.details.hospital,
              treatment: data.details.treatment,
              programme: data.details.programme,
              labelList: data.details.labelList,
            })
            data.fileUrl = data.fileList
            delete data.details
            delete data.videoUrl
            delete data.fileList
          }

          this.config[this.type]?.func(data).then(() => {
            this.$modal.msgSuccess("提交成功");
            this.visible = false;
            this.$emit("getTableList");
          });
        } else {
          return false;
        }
      });
    },

    cancel() {
      this.visible = false;
      this.fileList = [];
    },

    // 表单重置
    reset() {
      this.form = markForm();
      this.$forceUpdate();
      this.fileList = [];
    },

  },
};
</script>
<style scoped lang="scss">
.label-list{
  height: 36px;
  display: flex;
  align-items: center;

  .el-tag{
    margin-right: 10px;
  }

  .button-new-tag{
    height: 28px;
    padding: 0 20px;
  }

  .input-new-tag{
    width: 100px;
    height: 28px;
    display: flex;

    ::v-deep .el-input__inner {
      width: 100%;
      height: 100%;
    }
  }
}
.person{
  position: relative;

  .el-icon-user{
    position: absolute;
    top: 10px;
    right: 10px;
    cursor: pointer;
  }

  .cover{
    width: 100%;
    height: 100%;
    position:absolute;
    top: 0;
    left: 0;
    z-index: 1;
    cursor: pointer;

    &:hover {
      .el-input__inner{
        border-color: #C0C4CC;
      }
    }
  }
}
</style>
